import com.alibaba.fastjson.JSON;
import com.vichat.room.vo.RoomVO;
import com.vichat.user.vo.RequestParamsVO;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by user on 2017/12/10.
 */
public class apiHttpPost {
    private static Log logger = LogFactory.getLog(apiHttpPost.class);
    private static String defaultUrl = "http://localhost:8888/roomApi/";
    //private static String defaultUrl = "http://47.100.112.4:6100/roomApi/";
    public static void main(String[] args){
        //获取频道信息
        //getRooms();
        //创建频道接口
        //createRoom();
        String beacon = "D4:22:02:00:13:BA";
        String response = beacon.replaceAll(" ","").replaceAll(":","");
        System.out.print(response);
    }

    /**
     * 获取频道
     */
    public static void getRooms(){
        String url = defaultUrl + "getRooms";
        //paramsVO对象
        RequestParamsVO paramsVO = new RequestParamsVO();
        paramsVO.setUid(300001067);
        paramsVO.setVersion(1513218879837l);
        paramsVO.setPage(1);
        paramsVO.setRows(10);
        String json = JSON.toJSONString(paramsVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
    }

    /**
     * 创建频道接口
     */
    public static void createRoom(){
        String url = defaultUrl + "createRoom";
        List<Long> uids = new ArrayList<>();
        RoomVO roomVO = new RoomVO();
        roomVO.setUid(300000003);
        roomVO.setName("临时频道测试");
        roomVO.setStyle(1l);
        uids.add(300000004l);
        uids.add(300000005l);
        uids.add(300000006l);
        roomVO.setUids(uids);
        roomVO.setExpireType(1l);
        roomVO.setHour(24);
        String json = JSON.toJSONString(roomVO);
        logger.info("输入参数：" + json);
        String responseBody = post(url,json);
        logger.info("输出结果：" + responseBody);
    }

    /**
     * post 方法
     * @param url
     * @param json
     * @return
     */
    private static String post(String url, String json) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost(url);
        String responseBody = null;
        try {
            // 建立一个NameValuePair数组，用于存储欲传送的参数
            method.addHeader("Content-type", "application/json; charset=utf-8");
            method.setHeader("Accept", "application/json");
            method.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
            long startTime = System.currentTimeMillis();
            HttpResponse response = httpClient.execute(method);
            long endTime = System.currentTimeMillis();
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            logger.info("调用API 花费时间(单位：毫秒)：" + (endTime - startTime));
            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed:" + response.getStatusLine());
            }
            // Read the response body
            responseBody = EntityUtils.toString(response.getEntity());
            //logger.info("输出参数：" + responseBody);
        } catch (IOException e) {
            // 网络错误
            e.printStackTrace();
        } finally {
            return responseBody;
        }
    }
}
